package com.voicenews.gcm;

import java.net.URL;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import com.voicenews.R;
import com.voicenews.activity.DashBoardActivity;
import com.voicenews.activity.ArticlesListAdapter;
import com.voicenews.playtts.ShowExtractedArticle;
import com.voicenews.utils.Utils;

public class ReceivedArticlesListShowsActivity extends DashBoardActivity {
	private ListAdapter adapter;
	private ListView listView;
	private List<JSONObject> objects = null;
	private String articlesJson = null;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		System.out.println("oncreate");
		setContentView(R.layout.received_news_list_show_layout);
		listView = (ListView) findViewById(R.id.receied_article_from_gcm_list);
		articlesJson = getIntent().getStringExtra("articlesJson");
		System.out.println("oncreate" + articlesJson);
		objects = Utils.convertToJSONObjects(articlesJson);
		adapter = new ArticlesListAdapter(this, objects);
		listView.setAdapter(adapter);

		setHeader("Tin Tức", true, true, false, false);

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				JSONObject json = (JSONObject) adapter.getItem(arg2);
				try {
					URL url = (URL) json.get("url");
					// Intent browserIntent = new
					// Intent(Intent.ACTION_VIEW,
					// Uri.parse(url.toString()));
					// startActivity(browserIntent);
					Intent i1 = new Intent(getApplicationContext(),
							ShowExtractedArticle.class);
					i1.putExtra("urlString", url.toString());
					i1.putExtra("title", json.get("title").toString());
					startActivity(i1);

				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

		});

	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		this.finish();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		System.out.println("destroy");
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		System.out.println("pause");
	}
	
	

}
